package com.smart.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.smart.consts.AppInteractiveState;
import com.smart.consts.AppStatusData;
import com.smart.dao.Page;
import com.smart.dao.PageRequest;
import com.smart.model.Loan;
import com.smart.model.LoanCond;
import com.smart.model.User;
import com.smart.model.UserViewRecord;
import com.smart.model.base.JsonModel;
import com.smart.util.StringUtil;

@Controller
public class AppLoanController extends BaseController{
	
	@RequestMapping("loan/getLoans")
	public void getLoans(String data,HttpServletRequest request, HttpServletResponse response) {
		JsonModel jsonModel = new JsonModel();
		AppStatusData statusObject = new AppStatusData();
		Page<Loan> list = null;
		LoanCond cond=new LoanCond();
		PageRequest pageRequest = new PageRequest();
		if (requestHandle(data, jsonModel,statusObject)) {
			String loanType = jsonModel.getBodyValue("loanType");
			String minQuota = jsonModel.getBodyValue("minQuota");
			String maxQuota = jsonModel.getBodyValue("maxQuota");
			String maxAverageApplyTime = jsonModel.getBodyValue("maxAverageApplyTime");
			String minAverageApplyTime = jsonModel.getBodyValue("minAverageApplyTime");
			String pageNubmerStr = jsonModel.getBodyValue("pageNubmer");
			String modal = jsonModel.getBodyValue("modal");
			String name = jsonModel.getBodyValue("nameLike");
			if(StringUtil.isNotEmpty(name)) {
				cond.setNameLike("%"+name+"%");
			}
			if(StringUtil.isNotEmpty(loanType)) {
				cond.setTagsLike("%"+loanType+"%");
			}
			if(StringUtil.isNotEmpty(minQuota)) {
				cond.setMinQuota(Integer.parseInt(minQuota));
				cond.addOrderByDesc("averageqQuota");
			}
			if(StringUtil.isNotEmpty(maxQuota)) {
				cond.setMaxQuota(Integer.parseInt(maxQuota));
			}
			if(StringUtil.isNotEmpty(maxAverageApplyTime)) {
				cond.setMaxAverageApplyTime(Integer.parseInt(maxAverageApplyTime));
			}
			if(StringUtil.isNotEmpty(minAverageApplyTime)) {
				cond.setMinAverageApplyTime(Integer.parseInt(minAverageApplyTime));
				cond.addOrderByDesc("averageApplyTime");
			}
			if(StringUtil.isNotEmpty(modal)) {
				if(modal.equals("recommend")) {
					cond.setRecommend(true);
				}else if(modal.equals("hot")) {
					cond.setHot(true);
				}else if(modal.equals("latest")) {
					cond.setLatest(true);
				}
				cond.addOrderByDesc("timeCreated");
			}
			if(StringUtil.isNotEmpty(pageNubmerStr)) {
				pageRequest.setPageNumber(Integer.valueOf(pageNubmerStr));
			}
			pageRequest.setPageSize(200);
			list = loanService.findAll(pageRequest,cond);
			statusObject = AppInteractiveState.SUCCESS_DATA;
			returnJson(response, statusObject, list);
		}
		returnJson(response,statusObject,null);
	}
	

	@RequestMapping("loan/showDetails")
	public void showDetails(String data,HttpServletRequest request, HttpServletResponse response) {
		JsonModel jsonModel = new JsonModel();
		AppStatusData statusObject = new AppStatusData();
		Loan loan = null;
		User user = null;
		//jsonModel.setHeaderValue("flag", "1");
		if (requestHandle(data, jsonModel,statusObject)) {
			String id = jsonModel.getBodyValue("id");
			String strUid = jsonModel.getBodyValue("uid");
			if(StringUtil.isNotEmpty(id)) {
				Long loanId = Long.valueOf(id);
				loan = loanService.findOne(loanId);
				if(StringUtil.isNotEmpty(strUid)) {
					user = userService.findOne(Long.valueOf(strUid));
				}
				if(loan!=null && user!=null) {
					saveViewRecord(user,loan);
				}
				statusObject=AppInteractiveState.SUCCESS_DATA;
			}else {
				statusObject = AppInteractiveState.EXCEPTION_DATA;
			}
			returnJson(response, statusObject, loan);
		}
		returnJson(response,statusObject,null);
	}
	private void saveViewRecord(User user, Loan loan) {
		UserViewRecord rec = new UserViewRecord();
		rec.setUserId(user.getId());
		rec.setLoanId(loan.getId());
		rec.setLoanIcon(loan.getIconUrl());
		rec.setLoanName(loan.getName());
		rec.setLoanQuota(loan.getAverageqQuota()+"");
		userViewRecordService.save(rec);
	}
}
